//引入组件库
var nf = sm("do_Notification");
var root = ui("$");
var do_App = sm("do_App");
var do_Page = sm("do_Page");
var hashdata = mm("do_HashData");
var mallUrl = require("url/mall");
var qqHttp = require("helper/http");
var helperOpen = require("helper/open");

var nolistdata = mm("do_ListData");
var listProductData = mm("do_ListData");
//UI
var listViewProduct=ui("listViewProduct");
listViewProduct.bindItems(listProductData);

var noList = ui("noList");
noList.bindItems(nolistdata);

var sendBtn = ui("sendBtn");

var sortALayout0 = ui("do_ALayout_0");
var sortALayout1 = ui("do_ALayout_1");
var sortALayout2 = ui("do_ALayout_2");
var sortALayout3 = ui("do_ALayout_3");
var sortALayoutArr = [sortALayout0,sortALayout1,sortALayout2,sortALayout3];

var sortLabel0 = ui("do_Label_0");
var sortLabel1 = ui("do_Label_1");
var sortLabel2 = ui("do_Label_2");
var sortLabel3 = ui("do_Label_3");
var sortLabelArr = [sortLabel0,sortLabel1,sortLabel2,sortLabel3];
var sortIndex = 0;

var icon0 = ui("icon0");
var icon1 = ui("icon1");
var icon2 = ui("icon2");
var icon3 = ui("icon3");
var sortIconArr = [icon0,icon1,icon2,icon3];
var sortIcon = ["source://image/common/asc01.png","source://image/common/asc02.png","source://image/common/asc03.png"];

var params={};
var search;
var pageNum = 1;
var totalPages = 1;
//头部
var addheader = ui("do_ALayout_header");
var textFieldSearch=ui("textFieldSearch")
//textFieldSearch.setFocus(true);

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

// 左上角-返回
var headerLeft = ui("headerLeft");

headerLeft.on("touch", function() {
	do_App.closePage()
});

//获取分类下的商品数据
getLstByCategory();
function getLstByCategory(){
	var data = do_Page.getData();
	if(data){
		textFieldSearch.setFocus(false);
		do_Page.hideKeyboard();
		if(data.categoryId){
			params.categoryId = data.categoryId;
		}
		if(data.supplierId){
			params.supplierId = data.supplierId;
		}
		if(data.name){
			search = data.name;
			textFieldSearch.text = search;
		}
		params.oidx = "DESC-saleCount";
		sortIconArr[0].source = sortIcon[2];
		loadData();
	}
}

textFieldSearch.on("enter", function(data, e) {
	sendSearch();
});

sendBtn.on("touch", function(data, e) {
	sendSearch();
});

function sendSearch(){
	do_Page.hideKeyboard();
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		loadData();
	}else{
		nf.toast("请输入商品关键字");
	}
}

function loadData(){
	pageNum = 1;
	params.name = search;
	params.page = pageNum;
	params.shopId = 1;//圈圈直供
	qqHttp.post(mallUrl.url.product.search, params, function(data) {
		if(null != data.contentSize && data.contentSize > 0){
			var mapData = data.productMap;
			totalPages = data.totalPages;
			listViewProduct.visible=true;
			noList.visible=false;
			listViewProduct.rebound();
			listProductData.removeAll();
			for(var i in mapData){
				listProductData.addOne({"template":0,"items":mapData[i]});
			}
			listViewProduct.refreshItems();
		}else{
			nolistdata.removeAll();
			listViewProduct.visible=false
			noList.visible=true;
			nolistdata.addData([{"template":0,"labelContent":"未找到您想要的商品哦"}]);
			noList.refreshItems();
		}
	});
}

var direction = "DESC";
sortALayoutArr.forEach(function(sort,tabIndex){
	sort.on("touch", function(data,e){
		do_Page.hideKeyboard();
		textFieldSearch.setFocus(false);
		if(sortIndex !== tabIndex){
			sortLabelArr[sortIndex].fontColor = "949EAAFF";//置灰
			sortLabelArr[tabIndex].fontColor = "FF7E00FF";//点亮
			sortIconArr[sortIndex].source = sortIcon[0];
			sortIconArr[tabIndex].source = sortIcon[2];
			direction = "DESC";
		}else{
			//再次点击
			if(direction == "DESC"){
				direction = "ASC";
				sortIconArr[tabIndex].source = sortIcon[1];
			}else{
				direction = "DESC";
				sortIconArr[tabIndex].source = sortIcon[2];
			}
		}
		sortIndex = tabIndex;
		params.oidx = direction+"-"+sortLabelArr[tabIndex].tag;
		listViewProduct.scrollToPosition(0, false);
		loadData();
	});
});

listViewProduct.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		loadData();
		listViewProduct.rebound();//复位【一定要做】
	}
});

listViewProduct.on("push", function(data){
	if (data.state == 2){
		getNextPageData();
		listViewProduct.rebound();//复位【一定要做】
	}
});

function getNextPageData(){
	pageNum++;
	if(totalPages >= pageNum){
	    params.page = pageNum;
	    qqHttp.post(mallUrl.url.product.search,params,function(data){
	    	if(null != data.contentSize && data.contentSize > 0){
				var mapData = data.productMap;
				listViewProduct.visible=true;
				noList.visible=false;
				listViewProduct.rebound();
				//listProductData.addData(data.page.content);
				for(var i in mapData){
					listProductData.addOne({"template":0,"items":mapData[i]});
				}
				listViewProduct.refreshItems();
			}
		});
	}else {
		nf.toast("已是最后一页");
		listViewProduct.rebound();
		listViewProduct.refreshItems();
	}
}